<?php
require_once(LIB.'/class_Categories.php');
if (!defined('TB_CATEGORIES')){ define('TB_CATEGORIES','categories');}

class admin_categories_controller {
    private $categories;
    private $structure;
    function __construct() {
        $this->fetch_data();
    }
    
    function fetch_data(){
        $categories=new categories();
        $categories->get_full_list();
        $cat_structure=$categories->open_categories_tree(0,0);
        array_shift($cat_structure);   
        $this->structure=$cat_structure;
        $this->categories=$categories->categories;                
    }
    
    //This function return the list of categories
    public function exec_index(){
        $data = array('category_name'=>'', 'parent_id'=>0,'category_priority'=>0,);
        $content = '<h1 style="margin:10px">Quản lý thư mục</h1>';
        $content .= $this->show_create_form($data);        
        $content .= $this->show_list();
        return array('content'=>$content);
    }
    
    public function exec_create(){
        //This function create a new users
        $status = GET_value('s');
        $data['category_name'] =  POST_value('category_name','');
        $data['category_u_name'] = VN_unaccent($data['category_name']);
        $data['parent_id'] = (int) POST_value('parent_id',0);
        $data['category_priority'] = (int) POST_value('category_priority',0);
        if ('submit' == $status){
            $r = Categories :: create($data);
            if ($r['success']){
                $content='<div class="success" style="margin:10px">Group '.$data['category_name'].' is successfully created</div>';
                $this->fetch_data();
                $index = $this->exec_index();
                $content.=$index['content'];
            }else{
                $content = $this->show_create_form($data, $r['error']);
            }
        }else{
            //Just show form
            $content = $this->show_create_form($data, null);
        }
        
        return array('content'=>$content);
    }
    
/////////
    public function exec_edit(){
        //This function edit a category
        $status = GET_value('s');
        $category_id = (int) GET_value('category_id',0);
        $data =array();
        $excluded_ids = $this->categories[$category_id]['descendants'];
        array_push($excluded_ids, $category_id); // Cannot set parent id to the current ID or children's ID!
        
        if ('submit' == $status){
            $data['category_id'] =  $category_id;
            $data['category_name'] =  POST_value('category_name','');
            $data['category_u_name'] = VN_unaccent($data['category_name']);
            $data['parent_id'] = (int) POST_value('parent_id',0);
            $data['category_priority'] = (int) POST_value('category_priority',0);
            $data['excluded_ids']=$excluded_ids;
            $r = Categories :: update($data);
            if ($r['success']){
                $content='<div class="success" style="margin:10px">Đã cập nhật thành công!</div>';
                $this->fetch_data();
                $index = $this->exec_index();
                $content.=$index['content'];
            }else{
                $content = $this->show_edit_form($data, $r['error']);
            }
        }else{
            //Just show form
            $r = Categories :: get_category($category_id);
            if (!$r['success']){
                $content = '<div class="error">'.$r['error'].'</div>';                                
            }else{
                $data=$r['data'];
                $data['excluded_ids']=$excluded_ids;
                $content = $this->show_edit_form($data, null);                
            }
        }
        
        return array('content'=>$content);
    }
    
    //Note: will need to update children's parent id to parent's id!
    // Also, news's category_id to perent_id
    public function exec_delete($category_id){
        
    } 
    
    //Private function
    private function show_list(){
        $tpl = new Template(MODULE_ROOT.'/admin_categories/view/index.html');
        $tpl->set('categories',$this->categories); 
        $tpl->set('structure',$this->structure); 
        return $tpl ->fetch();        
    }
    
    //Private function
    private function show_create_form($data,$error=''){
        $tpl = new Template(MODULE_ROOT.'/admin_categories/view/create_form.html');
        $tpl->set('categories',$this->categories); 
        $tpl->set('structure',$this->structure); 
        $tpl->set('data',$data); 
        $tpl->set('error',$error); 
        return $tpl ->fetch();        
    }
    
    //Private function
    private function show_edit_form($data,$error=''){
        $tpl = new Template(MODULE_ROOT.'/admin_categories/view/edit_form.html');
        $tpl->set('categories',$this->categories); 
        $tpl->set('structure',$this->structure); 
        $tpl->set('data',$data); 
        $tpl->set('error',$error); 
        return $tpl ->fetch();        
    }
    
}
